#ifndef _SQLITE_MANAGER_H_
#define _SQLITE_MANAGER_H_

#include "Thread.h"
#include <deque>
#include <string>
#include <sqlite3.h>

typedef void (*OnExecDone)(const std::string &userdata);
typedef int (*OnSelect)(void* data, int n_columns,char** column_values,char** column_names);

class CSQLiteManager : public SGLib::IRunnable
{
public:
    CSQLiteManager();
    virtual ~CSQLiteManager();
    bool Start();
    void Stop();
    virtual void Run();
    void Exec(const char *dbfile, const char *sql, std::string &userdata, OnExecDone onExecDone=NULL);
    void ExecSelect(const char *dbfile, const char *sql, std::string &userdata, OnSelect onSelect);
    // Exec sync
    void ExecSync(const char *dbfile, const char *sql, OnSelect onSelect=NULL);

private:
    struct sqlReq
    {
        sqlReq() : sqltype(0), dbfile(""), sql(""), userdata(""), onExecDone(NULL), onSelect(NULL)
        {}

        int sqltype;
        std::string dbfile;
        std::string sql;
        std::string userdata;
        OnExecDone onExecDone;
        OnSelect onSelect;
    };
    void _DoAddRequest(sqlReq &req);
    void _DoExec(sqlReq &req);

    SGLib::CEvent m_workSignal;
    SGLib::CThread *m_worker;
    bool m_isStop;

    SGLib::CLock m_lock;
    std::deque<sqlReq> m_reqQue;

    sqlite3  *m_db;
    std::string m_curdbfile;
};

#endif

